/**
 * Created by Administrator on 2017/12/12 0012.
 */
require(['jquery',"bbcare","signature",'scprit/patientsQuestionnaire/vm/common.js',
    'scprit/patientsQuestionnaire/vm/letterOfAgreement.servers.js'],function(jquery,$bb,signature,$common,$servers){
    var getInfo = JSON.parse(localStorage.getItem("patientInfo"));
    $("#signName").jSignature({height:"200", width: "100%",signatureLine:false,lineWidth:"1",'decor-color':'#fff'});
    $("#reset").click(function(){
        $("#signName").jSignature("reset");
    });
    //确定、
    var toSure = function(){
        getInfo.url = $(this).attr("data-url");
        //获取绘图笔画：
        var native = $("#signName").jSignature('getData', 'native');
        if(native.length == 0){
            $common.toastShow("请先阅读知情同意书并在空白处签名！",2000);
            return ;
        }
        //将画布内容转成图片：
        var datapair = $("#signName").jSignature("getData", "image");
        var i = new Image();
        i.src = "data:" + datapair[0] + "," + datapair[1];
        var file = convertBase64UrlToBlob(i.src);
        file.name="sign.png";
        getInfo.deptId = "lczsdept001";
        var formData = new FormData();
        formData.append("file1", file, file.name);
        formData.append("deptId",getInfo.deptId);
        $servers.upLoadSignature(formData,upLoadSignatureCallback,getInfo);
    };
    /*
    * 保存结果：
    * */
    var toSaveSignatureCallback = function(data,params){
        if(data.success){
            params.evaType = "GLE";
            localStorage.setItem("patientInfo",JSON.stringify(params));
            $common.getHtml(params);
            delete params.url;
        }else{
            $common.toastShow(data.msg);
        }
    };
    /*
    * 查看签名结果：
    * */
    var toViewSignatureCallback = function(data){
        if(data.success){
           /* $("#aaa").html('<img src="'+data.data.fileList[0].imgNewUrl+'"/>');*/
        }
    };
    //上传图片成功回调函数：
    var upLoadSignatureCallback = function(data,params){
        if(data.success){
            /*
            //成功之后查看：
            var inParams1 = $.extend({},params);
            inParams1.fileList = [{"imgUrl":data.data}];
            $servers.toViewSignature(inParams1,toViewSignatureCallback,params);
            */
            //成功之后保存：
            params.deptId = "lczsdept001";
            var inParams = $.extend({},params);
            inParams.imgUrl = data.data;
            $servers.toSaveSignature(inParams,toSaveSignatureCallback,params);
        }else{
            $common.toastShow(data.msg,2000);
        }
    };
    //“确认”
    $("#toSure").click(toSure);

    function convertBase64UrlToBlob(urlData){
        var bytes=window.atob(urlData.split(',')[1]);        //去掉url的头，并转换为byte
        //处理异常,将ascii码小于0的转换为大于0
        var ab = new ArrayBuffer(bytes.length);
        var ia = new Uint8Array(ab);
        for (var i = 0; i < bytes.length; i++) {
            ia[i] = bytes.charCodeAt(i);
        }
        return new Blob( [ab] , {type : 'image/png'});
    }
});
